<?php

namespace app\admin\controller;

use app\admin\model\Config;
use app\admin\model\ConfigType;

/**
 * 配置管理
 * Class Configs
 * @package app\admin\controller
 */
class Configs extends Base
{

    //配置类型列表
    public function index(){
        if(input("?table")) {
            $keyword = input("keyword/s");
            $ConfigType = new ConfigType();
            $result["code"] = 0;
            $result['data'] = $ConfigType->configTypeList($keyword);
            $result['count'] = $ConfigType->configTypeListCount($keyword);
            return json($result);
        }
        return view('index',[
            'title'         =>  '配置类型列表',
        ]);
    }

    //添加修改配置类型
    public function typeAdd(){
        $type_id = input("type_id/d",0);
        $ConfigType = new ConfigType();
        if(request()->isPost()){
            $data = input("post.");
            $check =  [//验证规则
                ["type_name", "require", "类型名称不能为空"],
                ["config_type", "require", "类型关键字不能为空"],
            ];
            $msg = $ConfigType->checkData($data,$check);
            if ($msg === true) {
                if($ConfigType->checkFieldOnly("type_name",$data["type_name"],"type_id",$type_id)){
                    $result["code"] = 1;
                    $result["msg"] = '类型名称已存在';
                }else{
                    if($ConfigType->checkFieldOnly("config_type",$data["config_type"],"type_id",$type_id)){
                        $result["code"] = 1;
                        $result["msg"] = '类型关键字已存在';
                    }else{
                        if($ConfigType->mySave($data,"type_id",$type_id)){
                            $result["code"] = 0;
                            $result["msg"] = "操作成功！";
                            $result["layerUrl"] = url("Configs/index");
                        }else{
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }else{
            if(!empty($type_id)){
                //修改获取详情
                $dataInfo = $ConfigType->dataInfo("type_id",$type_id);
                $this->assign('dataInfo',$dataInfo);
            }
        }
        return view('typeAdd',[
            'title'         =>  '添加修改配置类型',
        ]);
    }

    //删除配置类型
    public function typeDel(){
        $config_type = input("config_type/s");
        $ConfigType = new ConfigType();
        if(!empty($config_type)){
            $Config = new Config();
            $getTypeNumber = $Config->getTypeNumber($config_type);
            if(!$getTypeNumber) {
                if ($ConfigType->del("config_type",$config_type,true)) {
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                    $result["url"] = url("Configs/index");
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败！";
                }
            }else{
                $result["code"] = 1;
                $result["msg"] = "操作失败，该配置类型下有（".$getTypeNumber."）个配置！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "config_type，参数为空!";
        }
        return json($result);
    }

    //配置列表
    public function configList(){
        $config_type = input("config_type/s");
        if(input("?table")) {
            $keyword = input("keyword/s");
            $Config = new Config();
            $result["code"] = 0;
            $result['data'] = $Config->configList($config_type,$keyword);
            $result['count'] = $Config->configListCount($config_type,$keyword);
            return json($result);
        }
        return view('configList',[
            'title'         =>  '配置列表',
            'config_type'   =>  $config_type,
        ]);
    }


    //添加修改配置
    public function configAdd(){
        $config_id = input("config_id/d",0);
        $config_type = input("config_type/s");
        $Config = new Config();
        if(request()->isPost()){
            $data = input("post.");
            $check =  [//验证规则
                ["config_name", "require", "配置值不能为空"],
                ["config_type", "require", "配置类型不能为空"],
            ];
            $msg = $Config->checkData($data,$check);
            if ($msg === true) {
                if($Config->checkFieldOnly("config_name",$data["config_name"],"config_id",$config_id)){
                    $result["code"] = 1;
                    $result["msg"] = '配置值已存在';
                }else{
                    if($Config->mySave($data,"config_id",$config_id)){
                        $result["code"] = 0;
                        $result["msg"] = "操作成功！";
                        $result["parentUrl"] = url("Configs/configList",["config_type"=>$config_type]);
                    }else{
                        $result["code"] = 10000;
                        $result["msg"] = "操作失败！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
            return json($result);
        }else{
            if(!empty($config_id)){
                //修改获取详情
                $dataInfo = $Config->dataInfo("config_id",$config_id);
                $this->assign('dataInfo',$dataInfo);
            }else{
                $this->assign('dataInfo',["config_type"=>$config_type]);
            }
        }
        return view('configAdd',[
            'title'         =>  '添加修改配置',
            'config_type'   =>  $config_type,
        ]);
    }

    //删除配置
    public function configDel(){
        $config_id = input("config_id/d",0);
        $config_type = input("config_type/s");
        if(!empty($config_id)){
            $Config = new Config();
            if ($Config->del("config_id",$config_id,true)) {
                $result["code"] = 0;
                $result["msg"] = "操作成功!";
                $result["login"] = url("Configs/configList",["config_type"=>$config_type]);
            } else {
                $result["code"] = 10000;
                $result["msg"] = "操作失败！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "config_id，参数为空!";
        }
        return json($result);
    }
}
